package at.chrenko.tu.vs.shared;

import java.io.Serializable;
import java.net.InetAddress;

/**
 * @author Thomas Chrenko - 0728121
 *
 * FileTask contains all the information needed for executing a task.
 *
 */
public class FileTask implements Serializable {

	private static final long serialVersionUID = -8082760177598401056L;
	
	private int id;

	private String fileName;
	
	private byte[] file;
	
	private String startScript;
	
	private TaskType taskType;
	
	private TaskState taskState;
	
	private Engine[] engines;
	
//	private int assignedPort;
	
//	private InetAddress assignedAddress;
	
	private int costs;
	
	private StringBuffer output;
	
	private boolean paid;
	
	private boolean distributed;
	
	private int amount;
	
	public int getId() {
		return id;
	}

	public String getFileName() {
		return fileName;
	}

	public byte[] getFile() {
		return file;
	}
	
	public String getStartScript() {
		return startScript;
	}
	
	public TaskType getTaskType() {
		return taskType;
	}
	
	public TaskState getTaskState() {
		return taskState;
	}
	
//	public int getAssignedPort() {
//		return assignedPort;
//	}
	
//	public InetAddress getAssignedAddress() {
//		return assignedAddress;
//	}
	
	public Engine getAssignedEngine(int engine) {
		return engines[engine];
	}
	
	public int getCosts() {
		return costs;
	}
	
	public String getOutput() {
		return output.toString().replaceFirst("\n", "");
	}
	
	public boolean isPaid() {
		return paid;
	}
	
	public boolean isDistributed() {
		return distributed;
	}
	
	public int getAmount() {
		return amount;
	}
	
	public boolean isEnginesEmpty() {
		return engines == null;
	}
	
	public int getEnginesSize() {
		if(engines == null)
			return -1;
		else
			return engines.length;
	}
	
	public void setStartScript(String startScript) {
		this.startScript = startScript;
	}
	
	public void setTaskState(TaskState taskState) {
		this.taskState = taskState;
	}
	
//	public void setAssignedPort(int assignedPort) {
//		this.assignedPort = assignedPort;
//	}
	
//	public void setAssignedAddress(InetAddress assignedAddress) {
//		this.assignedAddress = assignedAddress;
//	}
	
	public void setAssignedEngine(int engine, InetAddress assignedAddress, int assignedPort, String startScript) {
		this.engines[engine] = new Engine(assignedAddress, assignedPort, startScript);
	}
	
	public void setCosts(int costs) {
		this.costs = costs;
	}
	
	public void appendOutput(String output) {
//		this.output.append("\n");
		this.output.append(output);
	}

	public void setPaid(boolean paid) {
		this.paid = paid;
	}
	
	public void setDistributed(boolean distributed) {
		this.distributed = distributed;
	}
	
	public void setAmount(int amount) {
		this.amount = amount;
	}
	
	public void createEngines(int amount) {
		this.engines = new Engine[amount];
		for(int i=0; i<amount; i++)
			this.engines[i] = null;
	}
	
	public FileTask(int id, String filename, byte[] file, TaskType taskType, TaskState taskState) {
		this.id = id;
		this.fileName = filename;
		this.file = file;
		this.taskType= taskType;
		this.taskState = taskState;
		
		this.engines = null;
		this.costs = 0;
		this.output = new StringBuffer();
		this.paid = false;
		this.distributed = false;
		this.amount = 1;
	}
}
